package com.tetris.project;

import java.util.ArrayList;

import com.tetris.project.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import engine.Board;
import engine.Score;

public class GameOver extends Activity {
	
	final String PROFILES_FILE = "/data/data/com.tetris.project/profiles";
	int profileIndex, profileListLimit = 10;
	ArrayList<Score> profileList;
	AlertDialog alertDialog;
	EditText profName;
	HandleScores myScores;
	//String toastMessage = new String(getResources().getString(R.string.toast_message));
	//CharSequence toastMessage = "Is anyone there?";
	//Toast nameEmpty = Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT);
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(Shr_Res.TAG, "Start GameOver.OnCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameover);
		myScores = new HandleScores(this);
		profileList = new ArrayList<Score>();
		profileList = myScores.readProfiles(profileList, PROFILES_FILE);
		//toastMessage = new String(getResources().getString(R.string.toast_message));
		Log.d(Shr_Res.TAG, "End GameOver.OnCreate");
	}
	
	@Override
	public void onStart() {
		Log.d(Shr_Res.TAG, "Start GameOver.OnStart");
		super.onStart();
		Log.d(Shr_Res.TAG, "End GameOver.OnStart");
	}
	
	@Override
	public void onResume() {
		Log.d(Shr_Res.TAG, "Start GameOver.OnResume");
		super.onResume();
		Log.d(Shr_Res.TAG, "End GameOver.OnResume");
	}
	
	@Override
	public void onPause() {
		Log.d(Shr_Res.TAG, "Start GameOver.OnPause");
		super.onPause();
		Log.d(Shr_Res.TAG, "End GameOver.OnPause");
	}
	
	@Override
	public void onStop() {
		Log.d(Shr_Res.TAG, "Start GameOver.onStop");
		super.onStop();
		Log.d(Shr_Res.TAG, "End GameOver.onStop");
	}
	
	@Override
	public void onDestroy() {
		Log.d(Shr_Res.TAG, "Start GameOver.onDestroy");
		super.onDestroy();
		Log.d(Shr_Res.TAG, "End GameOver.onDestroy");
	}
	
	public void clickHandler(View view){
		switch (view.getId()){
		case R.id.gameover_yesButton:
			Log.d(Shr_Res.TAG, "Yes Button pressed");
			openSelectProfileDialog();
			break;
		case R.id.gameover_noButton:
			Log.d(Shr_Res.TAG, "No Button pressed");
			finish();
			break;
		case R.id.acceptProfile:
			Score sc = new Score();
			if ( profileIndex == 0 ) {
				sc.setName(profName.getText().toString());
				//sc.getName().substring(0, sc.getName().length() -1);
				Log.d("GameOver", "Profile typed was: "+sc.getName());
			}
			else {
				sc = profileList.get(profileIndex - 1);
				Log.d("GameOver", "Profile choose was: "+sc.getName());
			}
			sc.setScore(Board.score);
			Log.d("GameOver", "Score obtained was: "+sc.getScore());
			if(sc.getName().equalsIgnoreCase("")){
				Log.d("GameOver","Show Toast here to do it again");
				Toast.makeText(GameOver.this, "Type something or choose one!!", Toast.LENGTH_SHORT).show();
			}
			else{
				updateList(sc);
				alertDialog.dismiss();
				myScores.writeProfiles(profileList, PROFILES_FILE);
				finish();
			}
			break;
		}
	}
	
//	if(sc.getName().contains(null)){
//		Log.d("GameOver","Show Toast here to do it again");
	
	private void openSelectProfileDialog() {
		AlertDialog.Builder builder;
		Spinner s1;
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.profile_dialog, (ViewGroup) findViewById(R.id.layout_profile));
		builder = new AlertDialog.Builder(this);
		builder.setView(layout);
		alertDialog= builder.create();
		alertDialog.show();
		s1 = (Spinner) layout.findViewById(R.id.profileSpinner);
		profName = (EditText) layout.findViewById(R.id.profNameEdit); 
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.add("Add new profile");
		for ( int i = 0; i < profileList.size(); i++) {
			adapter.add(profileList.get(i).getName());
		}
		s1.setAdapter(adapter);
		s1.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				profileIndex = position;
				if ( position == 0) {
					profName.setVisibility(View.VISIBLE);
					}
				else {
					profName.setVisibility(View.GONE);
				}
			}
			public void onNothingSelected(AdapterView<?> parent) {
				//showToast("Spinner1: unselected");
				}
		});
	}
	
	private void updateList(Score sc){
		
		if ( profileIndex == 0 ){
			if(profileList.size() == profileListLimit){
				Log.d("GameOver", "The list is full");
				profileList.set(profileListLimit-1, sc);  //If there are profileListLimit scores already in the file, it will delete the last one
				Log.d("GameOver", "Replaced the last score");
			}
			else{
				profileList.add(sc);
				Log.d("GameOver", "New profile added to the list");
			}
		}
		else
		{
			Log.d("GameOver", "The profile is already in the list");
			profileList.get(profileIndex-1).setScore(sc.getScore());
			Log.d("GameOver", "Updated profile-> "+profileList.get(profileIndex-1).getName());
			Log.d("GameOver", "Updated score -> "+sc.getScore());
/*			for (int i=0; i<profileList.size(); i++){
				if(sc.getName().equalsIgnoreCase(profileList.get(i).getName())){
					Log.d("GameOver", "Profile found -> "+sc.getName());
					profileList.get(i).setScore(sc.getScore());
					Log.d("GameOver", "Updated score -> "+sc.getScore());
					break;
				}
			}*/
		}
		//profileList.equals(o);
		//profileList.add(index, object);
		for (int i=0; i<profileList.size(); i++){
			Log.d("GameOver", profileList.get(i).getName()+" "+profileList.get(i).getScore());
		}
		sortProfileList(profileList);
		for (int i=0; i<profileList.size(); i++){
			Log.d("GameOver", profileList.get(i).getName()+" "+profileList.get(i).getScore());
		}
	}
	
	private void sortProfileList(ArrayList<Score> profileList){
		Score scTemp = new Score();
		Log.d("GameOver", String.valueOf(profileList.size()));
		for (int j=0; j < profileList.size()-1; j++){
			for (int i=0; i < profileList.size()-1; i++){
				//Log.d("GameOver", String.valueOf(i));
				if (profileList.get(i).getScore()<profileList.get(i+1).getScore()){
					scTemp.setName(profileList.get(i+1).getName());
					scTemp.setScore(profileList.get(i+1).getScore());
					profileList.get(i+1).setName(profileList.get(i).getName());
					profileList.get(i+1).setScore(profileList.get(i).getScore());
					profileList.get(i).setName(scTemp.getName());
					profileList.get(i).setScore(scTemp.getScore());
				}
			}
		}
	}
}